<?php

$endpoint = 'http://giv-lidelbus.uni-muenster.de:8080/openrdf-sesame/repositories/transit?query=';
$prefix = '
	PREFIX transit:<http://vocab.org/transit/terms/>
	PREFIX dc:<http://purl.org/dc/elements/1.1/>
	PREFIX ns1:<http://www.purl.org/dc/elements/1.1/>
	PREFIX rdfs:<http://www.w3.org/2000/01/rdf-schema#>
	PREFIX ns4:<http://www.w3.org/2003/01/geo/wgs84_pos#>
	PREFIX ns3:<http://rdfpad.lodum.de/pad.ifgi.de/linkedtransit/>
	PREFIX owl:<http://www.w3.org/2002/07/owl#>
	PREFIX xsd:<http://www.w3.org/2001/XMLSchema#>
	PREFIX rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
	PREFIX vcard:<http://www.w3.org/2001/vcard-rdf/3.0#>
	PREFIX dcterms:<http://purl.org/dc/terms/>';
$result = "";

function query($selectQuery) {
	global $endpoint, $prefix;
	$query = $endpoint.urlencode($prefix.$selectQuery);

	$ch = curl_init($query);
	curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/sparql-results+json')); 
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

	$queryresult = curl_exec($ch);

	curl_close($ch);
	return $queryresult;
}

function isResultEmpty($result) {
	$json = json_decode(strval($result), true);
	return (count($json['results']['bindings'])==0);
}

//immer gleichbleibend 'head' und 'results' 'bindings'.
//und unter 'binding' stehen dann die objektnamen aus dem 'head'
function printTable($result) {
	//decode the result
	$json = json_decode(strval($result), true);
	$jsonhead = $json['head'];
	$jsonresults = $json['results'];
	$jsonbindings = $jsonresults['bindings'];
	//echo the table
	echo '<table border="1">';
	echo '<tr>';
	for ($x = 0; $x < count($jsonhead['vars']); $x++) {
	//create header of table
	echo '<th><b>'.utf8_decode($jsonhead['vars'][$x]).'</b></th>';
	}
	echo '</tr>';
	//fill table
	for ($y = 0; $y < count($jsonbindings);$y++) {
		echo '<tr>';
		for ($x = 0; $x < count($jsonhead['vars']); $x++) {
			echo '<td>';
			echo utf8_decode($jsonbindings[$y][$jsonhead['vars'][$x]]['value']);
			echo '</td>';		
		}
		echo '</tr>';
	}

	echo '</table>';
}

?>
